package com.koocloud.electroplatemanage.service.impl;

import cn.hutool.core.lang.Validator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.mapper.StatReportMapper;
import com.koocloud.electroplatemanage.pojo.*;
import com.koocloud.electroplatemanage.service.StatReportService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @program: electroplatemanage
 * @description: 报表统计实现类
 * @author: xxc
 * @create: 2020-10-13 09:50
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class StatReportServiceImpl implements StatReportService {
    @Resource
    private StatReportMapper statReportMapper;

    /**
     *@Description:整体按月完成情况
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/13
     */
    @Override
    public ResponseTemplate selectAllMonthStatus(String year) {

        try {
            List<StatAllResult> statAllResultList = new ArrayList<>();
            for (int i = 1;i<=12;i++ ){
                StatAllResult statAllResultEmtry = new StatAllResult();
                statAllResultEmtry.setMonths(i);
                statAllResultEmtry.setCount(0);
                statAllResultEmtry.setPrice(0.0000);
                statAllResultEmtry.setTerms(0);
                statAllResultEmtry.setTimes(0.0000);
                statAllResultList.add(statAllResultEmtry);
            }
            //整体按月完成情况统计
            List<StatAllResult> statAllResults = statReportMapper.selectAllMonthStatus(year);
            int sumTerms = 0;
            int sumCount = 0;
            double sumPrice = 0;
            double sumTimes = 0;
            for (StatAllResult statAllResult:statAllResults) {
                for (int i = 0;i < 12;i++ ){
                    if (statAllResultList.get(i).getMonths().equals(statAllResult.getMonths())){
                        statAllResultList.get(i).setTimes(statAllResult.getTimes());
                        statAllResultList.get(i).setTerms(statAllResult.getTerms());
                        statAllResultList.get(i).setPrice(statAllResult.getPrice());
                        statAllResultList.get(i).setCount(statAllResult.getCount());
                    }
                }
                sumTerms +=statAllResult.getTerms();
                sumCount +=statAllResult.getCount();
                sumPrice +=statAllResult.getPrice();
                sumTimes +=statAllResult.getTimes();
            }

            return ResponseTemplate.builder().code(0).message("查询成功").count(Long.valueOf(statAllResultList.size())).count(Long.valueOf(sumCount)).data(statAllResultList).build();
        }catch (Exception e){
            //事务手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            //遇到异常，返回异常原因
            e.printStackTrace();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }
    /**
     *@Description: 整体按周完成情况
     *@Param: [year]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/13
     */
    @Override
    public ResponseTemplate selectAllWeekStatus(String year,String page) {
        String limit = "12";
        //校验分页参数是否为纯数字 并且page和limit不都等于0
        if(Validator.isNumber(page)&&Validator.isNumber(limit)&&!("0".equals(page)&&"0".equals(limit))) {
            try {
                Integer Startlimit = Integer.valueOf(page)*12-11;// 1  1-12   2  13-24  3  25-36  4  37-48  5  49-60
                Integer endlimit = Integer.valueOf(page)*12;
                //整体按周完成情况统计
                List<StatAllWeekResult> statAllWeekResults = statReportMapper.selectAllWeekStatus(year);
                List<StatAllWeekResult> statAllWeekResultList = new ArrayList<>();
                for (int i = Startlimit; i >= Startlimit && i <= endlimit;i++){
                    StatAllWeekResult statAllWeekResult = new StatAllWeekResult();
                    statAllWeekResult.setWeeks(i);
                    statAllWeekResult.setTerms(0);
                    statAllWeekResult.setCount(0);
                    statAllWeekResult.setPrice(0.0000);
                    statAllWeekResult.setTimes(0.0000);
                    statAllWeekResultList.add(statAllWeekResult);
                }

                for (StatAllWeekResult statAllWeekResult: statAllWeekResults) {
                    for (int i = 0; i < 12;i++){
                        if (statAllWeekResultList.get(i).getWeeks().equals(statAllWeekResult.getWeeks())){
                            statAllWeekResultList.get(i).setTerms(statAllWeekResult.getTerms());
                            statAllWeekResultList.get(i).setCount(statAllWeekResult.getCount());
                            statAllWeekResultList.get(i).setPrice(statAllWeekResult.getPrice());
                            statAllWeekResultList.get(i).setTimes(statAllWeekResult.getTimes());
                        }
                    }
                }
                //开始分页
                PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
                //构造分页实体
                PageInfo<StatAllWeekResult> info = new PageInfo<>(statAllWeekResultList);
                if (info.getTotal() > 0) {
                    //查询到数据
                    return ResponseTemplate.builder().code(0).message("查询成功").count(Long.valueOf(info.getSize())).data(info.getList()).build();
                } else {
                    //没有查询到数据
                    return ResponseTemplate.builder().code(1).message("没有查到对应数据").count(0l).build();
                }

            } catch (Exception e) {
                //事务手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                //遇到异常，返回异常原因
                e.printStackTrace();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
        }else {
            return ResponseTemplate.builder().code(1).message("分页参数错误").count(0l).build();
        }

    }

    /**
     *@Description:个人按月完成项数
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/13
     */
    @Override
    public ResponseTemplate selectPersonMonthTerms(String year) {
        try {
            List<PersonMonths> personMonths = statReportMapper.selectPersonMonthTerms(year);
            /*List<PersonMonths> personMonthsList = new ArrayList<>();
            List<PersonMonths> personMonthsList1 = new ArrayList<>();

            for (PersonMonths personMonth:personMonths) {
               // if (personMonthsList.size() == 0){
                personMonthsList.add(personMonth);

                if (personMonthsList1.size() == 0 ) {
                    for (int i = 1 ; i <= 12;i++){
                        PersonMonths personMonthsEmtry = new PersonMonths();
                        personMonthsEmtry.setOperator(personMonth.getOperator());
                        personMonthsEmtry.setMonths(i);
                        personMonthsEmtry.setCount(0);
                        personMonthsEmtry.setPrice(0.0000);
                        personMonthsEmtry.setTerms(0);
                        personMonthsEmtry.setTimes(0.0000);
                        Iterator<PersonMonths> iterator = personMonthsList.iterator();
                        while(iterator.hasNext()) {
                            PersonMonths personMonths2 = iterator.next();
                            if (personMonths2.getOperator().equals(personMonthsEmtry.getOperator()) && !personMonths2.getMonths().equals(personMonthsEmtry.getMonths())){
                                personMonthsList1.add(personMonthsEmtry);
                            }
                        }
                    }
                }else {

                    if (personMonthsList1.stream().filter(w->String.valueOf(w.getOperator()).equals(personMonth.getOperator())).findAny().isPresent()){
                        //说明有
                        for (PersonMonths personMonths1:personMonthsList1){
                            if (personMonths1.getOperator().equals(personMonth.getOperator()) && personMonths1.getMonths().equals(personMonth.getMonths())){
                                personMonthsList1.remove(personMonths1);
                            }
                        }

                    }else {
                        for (int i = 1 ; i <= 12;i++){
                            PersonMonths personMonthsEmtry = new PersonMonths();
                            personMonthsEmtry.setOperator(personMonth.getOperator());
                            personMonthsEmtry.setMonths(i);
                            personMonthsEmtry.setCount(0);
                            personMonthsEmtry.setPrice(0.0000);
                            personMonthsEmtry.setTerms(0);
                            personMonthsEmtry.setTimes(0.0000);
                            Iterator<PersonMonths> iterator = personMonthsList.iterator();
                            while(iterator.hasNext()) {
                                PersonMonths personMonths2 = iterator.next();
                                if (!personMonths2.getOperator().equals(personMonthsEmtry.getOperator()) && !personMonths2.getMonths().equals(personMonthsEmtry.getMonths())){
                                    personMonthsList1.add(personMonthsEmtry);
                                }
                            }
                        }
                    }


                }


            }
            personMonthsList1.addAll(personMonthsList);*/
            return ResponseTemplate.builder().code(0).message("查询成功").count(Long.valueOf(personMonths.size())).data(personMonths).build();
        }catch (Exception e){
            //事务手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            //遇到异常，返回异常原因
            e.printStackTrace();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }
    /**
     *@Description:个人按月完成件数
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/13
     */
    @Override
    public ResponseTemplate selectPersonMonthCounts(String year) {
        try {
            List<PersonMonths> personMonths = statReportMapper.selectPersonMonthCounts(year);
            return ResponseTemplate.builder().code(0).message("查询成功").count(Long.valueOf(personMonths.size())).data(personMonths).build();
        }catch (Exception e){
            //事务手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            //遇到异常，返回异常原因
            e.printStackTrace();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }
    /**
     *@Description:个人按月完成件数含税总价
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/13
     */
    @Override
    public ResponseTemplate selectPersonMonthPrices(String year) {
        try {
            List<PersonMonths> personMonths = statReportMapper.selectPersonMonthPrices(year);
            return ResponseTemplate.builder().code(0).message("查询成功").count(Long.valueOf(personMonths.size())).data(personMonths).build();
        }catch (Exception e){
            //事务手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            //遇到异常，返回异常原因
            e.printStackTrace();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }
    /**
     *@Description:个人按月完成件数总工时
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/13
     */
    @Override
    public ResponseTemplate selectPersonMonthTimes(String year) {
        try {
            List<PersonMonths> personMonths = statReportMapper.selectPersonMonthTimes(year);
            return ResponseTemplate.builder().code(0).message("查询成功").count(Long.valueOf(personMonths.size())).data(personMonths).build();
        }catch (Exception e){
            //事务手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            //遇到异常，返回异常原因
            e.printStackTrace();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }

    /**
     *@Description: 个人按周完成项数
     *@Param: [year]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/14
     */
    @Override
    public ResponseTemplate selectPersonWeekTerms(String year) {
        try {
            List<PersonWeeks> personWeeks = statReportMapper.selectPersonWeekTerms(year);
            return ResponseTemplate.builder().code(0).message("查询成功").count(Long.valueOf(personWeeks.size())).data(personWeeks).build();
        }catch (Exception e){
            //事务手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            //遇到异常，返回异常原因
            e.printStackTrace();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }

    }
    /**
     *@Description: 个人按周完成件数
     *@Param: [year]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/14
     */
    @Override
    public ResponseTemplate selectPersonWeekCounts(String year) {
        try {
            List<PersonWeeks> personWeeks = statReportMapper.selectPersonWeekCounts(year);
            return ResponseTemplate.builder().code(0).message("查询成功").count(Long.valueOf(personWeeks.size())).data(personWeeks).build();
        }catch (Exception e){
            //事务手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            //遇到异常，返回异常原因
            e.printStackTrace();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }
    /**
     *@Description: 个人按周完成件数含税总价
     *@Param: [year]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/14
     */
    @Override
    public ResponseTemplate selectPersonWeekPrices(String year) {
        try {
            List<PersonWeeks> personWeeks = statReportMapper.selectPersonWeekPrices(year);
            return ResponseTemplate.builder().code(0).message("查询成功").count(Long.valueOf(personWeeks.size())).data(personWeeks).build();
        }catch (Exception e){
            //事务手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            //遇到异常，返回异常原因
            e.printStackTrace();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }
    /**
     *@Description: 个人按周完成件数总工时
     *@Param: [year]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/14
     */
    @Override
    public ResponseTemplate selectPersonWeekTimes(String year) {
        try {
            List<PersonWeeks> personWeeks = statReportMapper.selectPersonWeekTimes(year);
            return ResponseTemplate.builder().code(0).message("查询成功").count(Long.valueOf(personWeeks.size())).data(personWeeks).build();
        }catch (Exception e){
            //事务手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            //遇到异常，返回异常原因
            e.printStackTrace();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }
}
